package chaw.android.train;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import chaw.android.util.HttpRequestManager;
import chaw.android.util.LoadBitMap;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class DenyPeopleListActivity extends ListActivity{
	private ArrayList<PeopleItem> list;
	private PeopleAdapter nadapter;
	private ImageButton ok;
	private TextView title;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_people_list);
	    list = new ArrayList<PeopleItem>();
	    title = (TextView)findViewById(R.id.title);
	    title.setText("차단 관리");
		ok = (ImageButton)findViewById(R.id.btnOk);

		ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		}); 
		setFavoritePeople();
	}
	private void setFavoritePeople(){
		Intent intent = getIntent();
		String result = intent.getExtras().get("modify_list").toString();
		DownloadAsynkTask2 task = new DownloadAsynkTask2();
		task.execute(new String[]{result});
	}
	private class DownloadAsynkTask2 extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			String response = null;

			JSONObject json_obj;
			try {
				json_obj = new JSONObject(urls[0]);
				JSONArray json_arr = (JSONArray) json_obj.get("deny");
				for(int i = 0 ; i < json_arr.length(); i++){
					JSONObject users = (JSONObject) json_arr.get(i);
					Bitmap image = null;
					if(!users.get("image_thumb_url").toString().equals("") && !users.get("image_thumb_url").toString().equals("null")){	
						image = LoadBitMap.fromURL(users.get("image_thumb_url").toString());
					}
					list.add(new PeopleItem(users.get("nick_name").toString(), image , users.get("user_id").toString()));
				}	
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
	        
			nadapter = new PeopleAdapter(getBaseContext(), R.layout.modify_people_row, list);
		    ListView lv = getListView();
		    lv.setAdapter(nadapter);
		}
	}




	private class UploadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
	}
	
	private class PeopleAdapter extends ArrayAdapter<PeopleItem>{
		private ArrayList<PeopleItem> items;
		private int layout;
		
		public PeopleAdapter(Context ctx, int rsrcId, ArrayList<PeopleItem> data){
			super(ctx, rsrcId, data);
			this.items = data;
			this.layout = rsrcId;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent){
			View v = convertView;
			if(v == null){
				LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(layout, null);
			}
			final PeopleItem e = items.get(position);
			if(e != null){
				ImageView image = ((ImageView)v.findViewById(R.id.userPic));
				if(e.getImage() !=null){
					image.setImageBitmap(e.getImage()) ;
				}
				((TextView)v.findViewById(R.id.nickName)).setText(e.getName());
				((TextView)v.findViewById(R.id.btnText)).setText("해제");
				((ImageButton)v.findViewById(R.id.btnDelete)).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						list.remove(position);
						UploadAsynkTask task = new UploadAsynkTask();
						String[] params = new String[]{ ServerURL.moveDenyToFavorite, "user_id", e.getId(),
									"favorite_id", Preferences.getValue(getBaseContext(), "favorite_id"),
									"deny_id" , Preferences.getValue(getBaseContext(), "deny_id"),
									"user_id_check", Preferences.getValue(getBaseContext(), "user_id"),
									"uuid_check", Preferences.getValue(getBaseContext(), "uuid")};
						task.execute(params);
						nadapter.notifyDataSetChanged();
					}
				});
			}
			return v;
		}
	}

	public class PeopleItem{
		private String nick_name;
		private Bitmap image_url;
		private String user_id;
		
		private PeopleItem(String name, Bitmap url, String id){
			nick_name = name;
			image_url = url;
			user_id = id;
		}
		private String getName(){
			return nick_name;
		}
		private Bitmap getImage(){
			return image_url;
		}
		private String getId(){
			return user_id;
		}
	}
	
}
